package com.apestech.framework.run.datamapper;

import com.apestech.formwork.run.core.ExecUtil;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.ExceptionUtil;
import com.apestech.framework.util.MathUtil;
import com.apestech.framework.util.StringFuncUtil;

import java.util.ArrayList;

/**
 * 待执行的数据列表JSONArray返回值
 *
 * @author hup
 * @create 2018-03-30-16:14
 */
public class MappingData extends StringFuncUtil{

    private ArrayList<SingleDataMappingModule> params=new ArrayList<SingleDataMappingModule>();

    public void add(String field_id, String target, String field_name,
                    String field_type, String allow_null, String default_value, String field_check,
                    String value, String tableName, String express, String math_id, int math_size){
        SingleDataMappingModule module=new SingleDataMappingModule();
        module.setField(field_id, target, field_name,
                field_type, allow_null, default_value, field_check,
                value, tableName, express, math_id, math_size);
        this.params.add(module);
    }

    public void add(String tableName, String fieldName, String target, String express){
        SingleDataMappingModule module=new SingleDataMappingModule();
        module.setField(fieldName, target, null,
                null, null, null, null,
                null, tableName, express, null, 0);
        this.params.add(module);
    }

    public void add(String text){
        if(text==null) return;
        text=text+";";
        String[] list=text.split(";");
        int len=list.length;
        for(int i=0; i<len; i++){
            if(list[i]!=null){
                StrSingleData data=new StrSingleData(list[i]);
                add(data.getTableName(), data.getFieldName(), data.getTarget(), data.getExpress());
            }
        }
    }

    public void clear(){
        this.params.clear();
    }

    public ArrayList<SingleDataMappingModule> getParams() {
        return params;
    }

    public void setParams(ArrayList<SingleDataMappingModule> params) {
        this.params = params;
    }

    public JSONObject getData(JSONObject data) throws Exception {
        JSONObject out=new JSONObject();
        JSONObject input=new JSONObject();
        if(data!=null){
            input.putAll(data);
        }
        int len=this.params.size();
        for(int i=0; i<len; i++){
            SingleDataMappingModule module=this.params.get(i);
            String target= ExecUtil.getDataValue(module.getTarget(), data);
            Object dvalue=ExecUtil.getDataValue(module.getValue(), data);
            if(data.get(target)!=null){
                dvalue=data.get(target);
                target=module.getField_id();
            }
            if(dvalue!=null){
                out.put(module.getField_id(), dvalue);
                input.put(module.getField_id(), dvalue);
            } else {
                Object value=input.get(module.getField_id());
                if(value==null && module.getDefault_value()!=null){
                    value=module.getDefault_value();
                }
                if(value==null){
                    if("N".equals(module.getAllow_null())){
                        ExceptionUtil.exception(this, "getSingleKey", module.getField_name()+"不允许为空!");
                    }
                }
                if(module.getExpress()!=null){
                    value=doExpression(ExecUtil.getDataValue(module.getExpress(),data), input);
                }
                if(module.getMath()!=null){
                    String mathid=module.getMath().getId();
                    int size=module.getMath().getSize();
                    if("ceil".equals(mathid)){
                        value= MathUtil.ceil(castToDouble(value) , size);
                    } else if("round".equals(mathid)){
                        value= MathUtil.round(castToDouble(value), size);
                    } else if("floor".equals(mathid)){
                        value= MathUtil.floor(castToDouble(value), size);
                    }
                }
                if(module.getField_type()!=null){
                    switch(module.getField_type()){
                        case "string":
                            value=castToString(value);
                            break;
                        case "int":
                            value=castToInt(value);
                            break;
                        case "long":
                            value=castToLong(value);
                            break;
                        case "double":
                            value=castToDouble(value);
                            break;
                        case "date":
                            value=castToDate(value);
                            break;
                        case "boolean":
                            value=castToBoolean(value);
                            break;
                        default:
                            break;
                    }
                }
                if(module.getField_check()!=null){
                    boolean result= (boolean) doExpression(module.getField_check(), input);
                    if(!result){
                        ExceptionUtil.exception(this, "getSingleKey", module.getField_name()+"数据校验失败!");
                    }
                }
                input.put(target, value);
                out.put(target, value);
            }
        }
        return out;
    }
}
